package com.itheima.controller;

import com.itheima.common.Com.Dish;
import com.itheima.common.pojo.PageInfo;
import com.itheima.common.pojo.Result;
import com.itheima.common.vo.DishDto;
import com.itheima.common.vo.DishVo;
import com.itheima.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 86188
 */
@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private DishService dishService;

    @GetMapping("/list")
    //@RequestParam 使用在springMVC接收参数用的
    public Result getList(@RequestParam("categoryId") String categoryId) {
        List<DishVo> list = dishService.getList(categoryId);
        return Result.success(list);
    }

    @PostMapping
    public Result save(@RequestBody DishVo dishVo) {
        dishService.save(dishVo);
        return Result.success("新增菜品成功");
    }

    @PutMapping
    public Result update(@RequestBody DishDto dishDto) {
        dishService.update(dishDto);
        return Result.success("修改菜品成功");
    }

    @DeleteMapping
    public Result delete(@RequestParam("ids") String ids) {
        dishService.delete(ids);
        return Result.success("删除成功");
    }

    @PostMapping("/status/{status}")
    public Result discontinued(@PathVariable("status") Integer status, @RequestParam("ids") String ids) {

        dishService.discontinued(status, ids);
        return Result.success("操作成功");
    }

    @GetMapping("/page")
    public Result getpage(String name,
                          @RequestParam(defaultValue = "1") Integer page,
                          @RequestParam(defaultValue = "10") Integer pageSize,
                          Integer type) {

        PageInfo<Dish> getpage = dishService.getpage(name, page, pageSize, type);
        return Result.success(getpage);
    }

    @GetMapping("{id}")
    public Result getById(@PathVariable("id") String id) {
        DishVo byId = dishService.getById(id);

        return Result.success(byId);
    }

}
